<?php
require_once dirname(__FILE__) . '/../login/LoginHandler.php';
require_once dirname(__FILE__) . '/Users.php';

class UsersModel {	
	private $database = NULL;
	
	public function __construct(Database $db) {
			$this->database = $db;
	}
	
	public function AddUser($user) {
		if ($this->UserExists($user->username)) {
			return false;
		}
				
		$sql = "INSERT INTO `users`.`user` (`username`,`fullname`, `password`, `email`) VALUES (?, ?, ?, ?);";
		$stmt = $this->database->prepare($sql);
		$stmt->bind_param("ssss", $user->username, $user->fullname, $user->password, $user->email);
		$this->database->ExecuteNonQuery($stmt);
			   
		return true;
	}
	
	public function GetUsers($username = null) {
		$stmt = null;
		
		if ($username == null) {
			$sql = "SELECT `username`, `fullname`, `password`, `email` From `users`.`user`";
			$stmt = $this->database->prepare($sql);
		} else {
			$sql = "SELECT `username`, `fullname`, `password`, `email` From `users`.`user` WHERE `username` =?";
			$stmt = $this->database->prepare($sql);
			$stmt->bind_param("s", $username);
		}
		$users = $this->database->GetManyBindToObjects($stmt, "User");
				
		return $users;
	}
	
	public function UserExists($username) {
		$sql = "SELECT EXISTS(SELECT * FROM User WHERE username=?)";
		$stmt = $this->database->prepare($sql);
		$stmt->bind_param("s", $username);
		$exists = $this->database->SelectOne($stmt);
		
		return $exists;
	}
	
	public function UpdateUser($username, $password, $newUsername, $newFullName, $newPassword = NULL, $newEmail = NULL) {
		$lh = new LoginHandler($this->database);
		if ($lh->Authenticate($username, $password)) {
			
			if ($newUsername == NULL) {
				$newUsername = $username;
			}
			if ($newPassword == NULL) {
				$newPassword = $password;
			}
			
			$sql = "UPDATE `users`.`user` SET `password` =?, `fullname` =?, `username` =? WHERE `user`.`username` =?;";
			$stmt = $this->database->prepare($sql);
			$stmt->bind_param("ssss", $newPassword, $newFullName, $newUsername, $username);
			$this->database->ExecuteNonQuery($stmt);
			return true;
		}
		return false;
	}
	
	public function RemoveUser($username) {
		if ($this->UserExists($username)) {
			$sql = "DELETE FROM `user` WHERE `username` =?;";
			$stmt = $this->database->prepare($sql);
			$stmt->bind_param("s", $username);
			$this->database->ExecuteNonQuery($stmt);
			return true;
		}
		return false;
	}
	
	public function Test($lh) {
		$this->RemoveUser('testuser');
		
		if ($this->UserExists('testuser')) {
			echo 'Fel! Användare "testuser" bör inte finnas.';
			return false;
		}
		
		$user = User::create('testuser', 'Test User', 'pwd', 'testuser@example.com');
		
		$addSuccessfull = $this->AddUser($user);
		
		if (!$addSuccessfull) {
			echo 'Fel! Adduser returnerade false trots att användaren inte fanns sedan tidigare';
			return false;
		}
		
		$lh->DoLogout();
		$lh->DoLogin('testuser', 'pwd');
		
		if (!$lh->IsLoggedIn()) {
			echo 'Fel! Det gick inte att logga in den nyskapade användaren "testuser".';
			return false;
		}
		
		$this->UpdateUser('testuser', 'pwd', 'test' , 'New Full Name', 'pass');
		
		$lh->DoLogout();
		$lh->DoLogin('test', 'pass');
		
		if (!$lh->IsLoggedIn()) {
			echo 'Fel! Den uppdaterade användaren "test" kunde inte loggas in.';
			return false;
		}
		
		if (!$this->UserExists('test')) {
			echo 'Fel! Användare "test" bör nu finnas.';
			return false;
		}
		
		$removeSucessfull = $this->RemoveUser('test');
		
		if (!$removeSucessfull) {
			echo 'Fel! RemoveUser("test") returnerade false trots att användaren bör kunna raderas.';
			return false;
		}
		
		if ($this->UserExists('test')) {
			echo 'Fel! Användare "test" bör inte finnas längre.';
			return false;
		}
		
		return true;
	}
}
	